package com.example.mybatis_demo.service;

import com.example.mybatis_demo.component.JwtUtil;
import com.example.mybatis_demo.entity.UserEntity;
import com.example.mybatis_demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Service
public class UserService {

    @Resource
    private JwtUtil jwtUtil;

    @Autowired
    private UserMapper userMapper;//接口直接被代理填充了，根据的就是xml文件中的配置代码

    public List<UserEntity> getAll(){
        return userMapper.getAll();// 调用接口中的“方法”
    }

    public String login(UserEntity userEntity) throws Exception {
        UserEntity user = userMapper.login(userEntity);
        if (user == null) {
            return null;
        }
        //如果能查出，则表示账号密码正确，生成jwt返回
        String uuid = UUID.randomUUID().toString().replace("-", "");
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", user.getUsername());
        return jwtUtil.createJWT(uuid, "login subject", 0L, map);
    }
}
